#!/usr/bin/env bash
# 依赖工具: dpkg sed
# 

# 全局变量
self_dir=$(cd "$(dirname "$0")"; pwd)
root_dir="$self_dir/.."
self_name=`basename $0`
pgm_user=smt
pgm_group=smt
pkg_name=mss
pkg_ver=`cat VERSION`
pkg_arch=`dpkg --print-architecture`
pkg_desc="BBNMS mss" 
prefix="/usr/local"

# 帮助
usage()
{
    printf "\n使用：\n"
    printf "\t%-23s %s\n" "./$self_name <options>"
    printf "\n选项：\n"
    printf "\t%-23s %s\n" "all"              "全部打包"
    printf "\t%-23s %s\n" "amd64"            "amd64平台打包"
    printf "\t%-23s %s\n" "arm64"            "arm64平台打包"
    printf "\t%-23s %s\n" "armhf"            "armhf平台打包"
    printf "\t%-23s %s\n" "i386"             "i386平台打包"
    printf "\t%-23s %s\n" "clean"            "清理deb包"
    printf "\n例子：\n"
    printf "\t%-23s %s\n" "./$self_name all"
}

# 检查入口参数
if [ "$#" -ne 1 ]; then
   usage;
   exit 0;
fi

# 替换变量
subst() 
{
    if [ "$#" -ne 1 ]; then
        printf "\n======== 参数为空 ========\n"
        return;
    fi
    
    replaces=(
        "s:@prefix@:${prefix}:g" 
        "s:@PKG_DESC@:${pkg_desc}:g" 
        "s:@PKG_ARCH@:${pkg_arch}:g" 
        "s:@PACKAGE_NAME@:${pkg_name}:g" 
        "s:@PACKAGE_VERSION@:${pkg_ver}:g" 
        "s:@pgm_user@:${pgm_user}:g" 
        "s:@pgm_group@:${pgm_group}:g"
    )

    targetfile=$1
    for rp in "${replaces[@]}"; 
    do 
        # printf "%s\n" "${rp}"
        sed -i "${rp}" "${targetfile}"
    done
}

# deb打包
pkgdeb() 
{
    if [ -d $root_dir/debroot ]; then 
        rm -rf $root_dir/debroot;
    fi

    echo "@prefix@/@PACKAGE_NAME@/configs/app.cfg" > ./configs/DEBIAN/conffiles.in
    for file in `ls ./drivers`;  do
        if [ "${file##*.}"x = "yml"x ] ;    then
            echo "@prefix@/@PACKAGE_NAME@/drivers/$file" >> ./configs/DEBIAN/conffiles.in
        fi
    done

    mkdir $root_dir/debroot
    mkdir -p $root_dir/debroot/DEBIAN
    mkdir -p $root_dir/debroot/usr/local/${pkg_name}/configs
    mkdir -p $root_dir/debroot/usr/local/${pkg_name}/drivers
    mkdir -p $root_dir/debroot/usr/local/${pkg_name}/var/log
    mkdir -p $root_dir/debroot/etc/init.d/
    cp configs/DEBIAN/conffiles.in $root_dir/debroot/DEBIAN/conffiles
    cp configs/DEBIAN/control.in $root_dir/debroot/DEBIAN/control
    cp configs/DEBIAN/postinst.in $root_dir/debroot/DEBIAN/postinst
    cp configs/DEBIAN/preinst.in $root_dir/debroot/DEBIAN/preinst
    cp configs/DEBIAN/prerm.in $root_dir/debroot/DEBIAN/prerm
    cp $root_dir/init/debian/daemon.in $root_dir/debroot/etc/init.d/${pkg_name}
    cp $root_dir/configs/app.cfg $root_dir/debroot/usr/local/${pkg_name}/configs/app.cfg
    cp -r $root_dir/drivers/* $root_dir/debroot/usr/local/${pkg_name}/drivers
    cp $root_dir/build/${pkg_name} $root_dir/debroot/usr/local/${pkg_name}/${pkg_name}
    chmod +x $root_dir/debroot/etc/init.d/${pkg_name}

    # daemon
    subst $root_dir/debroot/etc/init.d/${pkg_name}

    # DEBIAN
    subst $root_dir/debroot/DEBIAN/conffiles
    subst $root_dir/debroot/DEBIAN/control
    subst $root_dir/debroot/DEBIAN/postinst
    subst $root_dir/debroot/DEBIAN/preinst
    subst $root_dir/debroot/DEBIAN/prerm


    fakeroot dpkg-deb -Z gzip -b $root_dir/debroot/ $root_dir/${pkg_name}_${pkg_ver}_${pkg_arch}.deb
    rm -rf $root_dir/debroot
}

# armhf平台打包
pkgdeb_armhf() {
    make clean 
    CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm make
    pkg_arch=armhf
    pkgdeb
}

# arm64平台打包
pkgdeb_arm64() {
    make clean 
    CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 make
    pkg_arch=arm64
    pkgdeb
}

# amd64平台打包
pkgdeb_amd64() {
    make clean 
    CGO_ENABLED=1 GOOS=linux GOARCH=amd64 make
    pkg_arch=amd64
    pkgdeb
}

# i386平台打包
pkgdeb_i386() {
    make clean 
    CGO_ENABLED=1 GOOS=linux GOARCH=386 make
    pkg_arch=i386
    pkgdeb
}

# 打包所有平台
pkgdeb_all() {
    pkgdeb_amd64
    pkgdeb_arm64
    pkgdeb_armhf
    pkgdeb_i386
}

while [ ! -z "$1" ]; do 
    case $1 in
        all)
            # 全部打包
            pkgdeb_all	          
        ;;
        amd64) 
            pkgdeb_amd64
        ;;
        arm64) 
            pkgdeb_arm64
        ;;
        armhf)
            pkgdeb_armhf
        ;;
        i386)
            pkgdeb_i386
        ;;
        clean)
            rm -f *.deb
        ;;
        *) usage; exit 0 ;;
    esac
    shift
done

exit 0


